<?php

namespace app\data\controller\user;

use app\data\service\UserAdminService;
use think\admin\Controller;
use think\facade\Cache;

/**
 * 司机信息管理
 * Class Admin
 * @package app\data\controller\user
 */
class Admin extends Controller
{
    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'data_record';

    /**
     * 司机信息管理
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        // 用户等级分组
        $ts['ta'] = ['vip' => '', 'name' => '全部用户', 'count' => 0];
        $this->total = $ts;

        // 设置页面标题
        $this->title = '司机记录表';
        // 创建查询对象
        $query = $this->_query($this->table)->order('id desc');
        // 数据筛选选项
        $this->type = ltrim(input('type', 'ta'), 't');
        $query->where('phone', '<>', '');

        // 数据查询分页
        if (input('output') == 'json') {
            $result = $query->like('phone,username')->equal('id,status,is_service_provider,pid,channel_id')->dateBetween('create_at')->page(true, false);
            $this->success('', $result);
        } else {
            $query->like('phone,username')->equal('id,status,is_service_provider,pid,channel_id')->dateBetween('create_at')->page();
        }
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _page_filter(array &$data)
    {
        foreach ($data as &$item) {
            $item = UserAdminService::instance()->formatUserField($item, false);
        }
    }

    /**
     * 表单结果处理
     * @param boolean $state
     */
    protected function _form_result(bool $state, array $data)
    {
        if ($state) {
            $this->success('保存成功！', 'javascript:history.back()');
        }
    }

    /**
     * 数据列表处理
     * @param array $data
     */
    protected function _teams_page_filter(array &$data)
    {
    }

    /**
     * 编辑用户
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 表单数据处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {
        $this->title = '司机管理';
        if ($this->request->isPost()) {
            // 检查登录账号是否出现重复
            if (!empty($data['phone'])) {
                $where = ['phone' => $data['phone']];
                if ($this->app->db->name($this->table)->whereNotIn('id', [$data['id']])->where($where)->count() > 0) {
                    $this->error("手机号已经存在，请使用其它手机号！");
                }
            }

            $cacheKey = config('cacheKey.uid2user') . $data['id'];
            Cache::delete($cacheKey);
        } else {

        }
    }

    /**
     * 删除
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->app->db->name($this->table)->where('id', input('id'))->delete();
        $cacheKey = config('cacheKey.uid2user') . input('id');
        Cache::delete($cacheKey);
        $this->success('删除成功');
    }
}